package com.szit.test.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.szit.test.biz.SaleBiz;
import com.szit.test.biz.impl.SaleBizImpl;
import com.szit.test.entity.PageIndexer;
import com.szit.test.entity.Sale;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ListTeacherServlet",value = "/sys/list")
public class ListSaleServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        // 获取分页页码
        String index = request.getParameter("pageIndex");
        // 定义分页器
        PageIndexer page = new PageIndexer(1,5,0);
        if(index != null){
            page.setPageIndex(Integer.parseInt(index));
        }

        // 调用逻辑层
        SaleBiz tbiz = new SaleBizImpl();
        List<Sale> teachers = tbiz.searchByPage(page);

        Map<String,Object> maps = new HashMap<String, Object>();
        maps.put("teachers",teachers);
        maps.put("page",page);

        //序列化为JSON格式的数据
        String jsonTeachers = JSON.toJSONStringWithDateFormat(maps,"yyyy年MM月dd日 HH:mm", SerializerFeature.WriteMapNullValue);

        PrintWriter out = response.getWriter();
        out.print(jsonTeachers);
        out.flush();
        out.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
